﻿using System;
using System.IO;
using System.Threading; 
using System.Drawing;
using System.Runtime.InteropServices; 
using System.Diagnostics;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using System.Text;

namespace UDPServer
{
    public class TYCardCmd 
    {
        byte[] cmdParam = null;
        public TYCardCmdMain Main
        {
            get;
            set;
        }
        public int Minor
        {
            get;
            set;
        }
        public int Cmd
        {
            get { return (int)Main << 8 | (int)Minor; }
            set
            {
                Main = (TYCardCmdMain)(value >> 8);
                Minor =  (value & 255);  //11111111
            }
        }

        public byte[] GetBytes()
        {
            int len = 4;
            if (cmdParam != null) len += cmdParam.Length;
            byte[] cmd = new byte[len]; 
            cmd[0] = 0x3C;  //'<'
            cmd[1] = (byte)Main;
            cmd[2] = (byte)Minor; 
            if (cmdParam != null)
            {
                for (int i = 0; i < cmdParam.Length; i++)
                {
                    cmd[3 + i] = cmdParam[i];
                }
            }
            cmd[len-1] = 0x3E;  //'>'  
            return cmd;
        }

        public TYCardCmd()
        {
        }
        public TYCardCmd(TYCardCmdMain main, TYCardCmdMinorCtrl minor)
        {
            Main = main;
            Minor = (int)minor;
        }
        public TYCardCmd(TYCardCmdMain main, TYCardCmdMinorFile minor)
        {
            Main = main;
            Minor = (int)minor;
        } 
        public TYCardCmd(int main, int minor)
        {
            Main = (TYCardCmdMain)main;
            Minor = minor;
        }
        public void AppendParam(byte[] param )
        {
            cmdParam = param;
        }

        public bool IsCmd(TYCardCmdMain main, int minor)
        {
            return (Main == main && Minor ==minor);
        }
        public bool IsCmd(byte main, byte minor)
        {
            return IsCmd((TYCardCmdMain)((int)main), (int)minor);
        }
    }

    
     
    /// <summary>
    /// 通用控制卡主命令
    /// </summary>
    public enum TYCardCmdMain
    {
        None = 0,
        /// <summary>
        /// 控制命令(通用命令)
        /// </summary>
        Ctrl = 1,
        /// <summary>
        /// 查询命令
        /// </summary>
        File,
        
    }
    /// <summary>
    /// 通用控制卡次位命令
    /// </summary>
    public enum TYCardCmdMinorCtrl
    {
        None = 0,
        /// <summary>
        /// 启动命令 
        /// </summary>
        Start = 1,
        /// <summary>
        /// 停止命令
        /// </summary>
        Stop,
        /// <summary>
        /// 暂停命令
        /// </summary>
        Pause, 
        /// <summary>
        /// 恢复命令
        /// </summary>
        Resume,
        /// <summary>
        /// 回零命令
        /// </summary>
        BackHome, 
        /// <summary>
        /// 状态查询命令
        /// </summary>
        State,
        /// <summary>
        /// 扫描命令
        /// </summary>
        MoveScan,
        /// <summary>
        /// 扫描命令1
        /// </summary>
        MoveScan1,


        /// <summary>
        /// X正向移动
        /// </summary>
        XMovePositive,
        /// <summary>
        /// X负向移动
        /// </summary>
        XMoveNegtive,
        /// <summary>
        /// Y正向移动
        /// </summary>
        YMovePositive,
        /// <summary>
        /// Y负向移动
        /// </summary>
        YMoveNegtive,
        /// <summary>
        /// Z正向移动
        /// </summary>
        ZMovePositive,
        /// <summary>
        /// Z负向移动
        /// </summary>
        ZMoveNegtive,
        /// <summary>
        /// 正向移动U
        /// </summary>
        UMovePositive,
        /// <summary>
        /// U负向移动
        /// </summary>
        UMoveNegtive,
        /// <summary>
        /// 压网
        /// </summary>
        PressMaterial,
        /// <summary>
        /// Z气缸
        /// </summary>
        ZCylinder,
        /// <summary>
        /// 风机
        /// </summary>
        Fan,

    }
    /// <summary>
    /// 通用控制卡次位命令
    /// </summary>
    public enum TYCardCmdMinorFile
    {
        None = 0,
        /// <summary>
        /// 控制命令(通用命令)
        /// </summary>
        Download = 1,
        /// <summary>
        /// 设置命令
        /// </summary>
        Upload,
      
    }
    
     
    public struct mytransform
    {

    }

    public class Struct_Transform
    {
        //struct转换为byte[]
        public static byte[] StructToBytes(object structObj)
        {
            int size = Marshal.SizeOf(structObj);
            IntPtr buffer = Marshal.AllocHGlobal(size);
            try
            {
                Marshal.StructureToPtr(structObj, buffer, false);
                byte[] bytes = new byte[size];
                Marshal.Copy(buffer, bytes, 0, size);
                return bytes;
            }
            finally
            {
                Marshal.FreeHGlobal(buffer);
            }
        }

        //byte[]转换为struct
        public static object BytesToStruct(byte[] bytes, Type strcutType)
        {
            int size = Marshal.SizeOf(strcutType);
            IntPtr buffer = Marshal.AllocHGlobal(size);
            try
            {
                Marshal.Copy(bytes, 0, buffer, size);
                return Marshal.PtrToStructure(buffer, strcutType);
            }
            finally
            {
                Marshal.FreeHGlobal(buffer);
            }
        }
    }
 
}
